﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using SDSystem.Models;
using System.Linq.Dynamic;
using CKFinder;

namespace SDSystem.Controllers
{
    public class RestaurantController : Controller
    {
        private ForumEntities db=new ForumEntities();
        // GET: /Restaurant/

        public ActionResult Index()
        {
            return View();
        }

        //Search Restaurant
        public ActionResult Search()
        {
            var store = X.GetCmp<Store>("RestaurantDataStore");
            Session["sRestaurantName"] = X.GetCmp<TextField>("txtRestaurantName").Text;
            Session["sStreet"] = X.GetCmp<TextField>("txtStreet").Text;
            Session["sCity"] = X.GetCmp<TextField>("txtCity").Text;
            store.Reload();
            return this.Direct();
        }

        public ActionResult ClearSearch()
        {
            var store = X.GetCmp<Store>("RestaurantDataStore");
            // Clear search here
            Session["sRestaurantName"] = X.GetCmp<TextField>("txtRestaurantName").Text="";
            Session["sStreet"] = X.GetCmp<TextField>("txtStreet").Text="";
            Session["sCity"] = X.GetCmp<TextField>("txtCity").Text="";
            store.Reload();
            return this.Direct();
        }

        //Create Restaurant
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(Restaurant rs)
        {
            if (ModelState.IsValid)
            {
                //var httpPostedFileBase = Request.Files[0];
                //if (httpPostedFileBase != null)
                //{
                //    httpPostedFileBase.SaveAs(Server.MapPath("~/") + "//Content//Upload//" + httpPostedFileBase.FileName);
                //    rs.Image = "/Content/Upload/" + httpPostedFileBase.FileName;
                //}
                db.Restaurants.AddObject(rs);
                db.SaveChanges();
                //X.Msg.Notify("Successfull", "Restaurant is saved").Show();
                return this.FormPanel();
            }
            //X.Msg.Notify("Failed", "No Restaurant is created");
            return this.FormPanel();
        }

        //Edit Restaurant
        public ActionResult Edit(int id)
        {
            Restaurant rs = db.Restaurants.FirstOrDefault(e => e.RestaurantId == id);
            return View(rs);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(Restaurant rs)
        {
            Restaurant rs1 = db.Restaurants.SingleOrDefault(e => e.RestaurantId == rs.RestaurantId);
            if (ModelState.IsValid)
            {
                db.Restaurants.Detach(rs1);
                db.Restaurants.Attach(rs);
                db.ObjectStateManager.ChangeObjectState(rs, EntityState.Modified);
                db.SaveChanges();
                return this.FormPanel();
            }
            return View(rs);
        }

        //Deatail Restaurant
        public ActionResult Details(int id)
        {
            Restaurant rs = db.Restaurants.SingleOrDefault(e => e.RestaurantId == id);
            ViewBag.MapsLocation = rs.Street + "," + rs.City + ",Việt Nam";
            return View(rs);
        }

        //Restaurant Data
        public ActionResult RestaurantData(StoreRequestParameters parameters)
        {
            string restaurantname = (String)Session["sRestaurantName"];
            string street = (String)Session["sStreet"];
            String city = (String)Session["sCity"];
            var result = (from restDB in db.Restaurants
                         select new
                                    {
                                        restDB.RestaurantId,
                                        restDB.Name,
                                        restDB.Image,
                                        restDB.Street,
                                        restDB.City
                                    }).Where(e => (string.IsNullOrEmpty(restaurantname) || e.Name.Contains(restaurantname)) &&
                                                  (string.IsNullOrEmpty(street) || e.Street.Contains(street)) &&
                                                  (string.IsNullOrEmpty(city) || e.City.Contains(city))); ;
            // Sorting
            if (!string.IsNullOrEmpty(parameters.SimpleSort))
            {
                result =
                    result.OrderBy(parameters.SimpleSort +
                                   (parameters.SimpleSortDirection == SortDirection.DESC ? " desc" : " asc"));
            }
            else
            {
                result = result.OrderBy(ent => ent.RestaurantId);
            }

            // Paging
            int limit = parameters.Limit;
            if ((parameters.Start + parameters.Limit) > result.Count())
            {
                limit = result.Count() - parameters.Start;
            }

            var rangeData = (parameters.Start < 0 || limit < 0) ? result : result.Skip(parameters.Start).Take(limit);
            return this.Store(rangeData, result.Count());
        }
    }
}
